package com.epfresh.supplier.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.epfresh.api.entity.RequestTag;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.utils.FormatUtil;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.OfferListEntity;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import com.epfresh.supplier.dialog.*;
/**
 * Created by wzh on 2016/5/6.
 */
public class OfferAdapter extends BaseAdapter implements View.OnClickListener{

    public Context context;
    public List<OfferListEntity.ContentBean.ProductListBean> list;
    private AdapterView.OnItemClickListener onItemClickListener;

    public OfferAdapter(Context context , List<OfferListEntity.ContentBean.ProductListBean> list) {
        this.context = context;
        this.list = list;
    }

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getCount() {
        return list != null ?list.size():0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_offer, null, false);

            vh.offer_time_remind = convertView.findViewById(R.id.offer_time_remind);

            vh.rl_store = convertView.findViewById(R.id.rl_store);
            vh.tv_offer_price_tail = convertView.findViewById(R.id.tv_offer_price_tail);
            vh.rl_offer_footer = convertView.findViewById(R.id.rl_offer_footer);
            vh.v_disable = convertView.findViewById(R.id.v_disable);
            vh.iv_offer_close = convertView.findViewById(R.id.iv_offer_close);
            vh.tv_offer_store = (TextView) convertView.findViewById(R.id.tv_offer_store);
            vh.tv_offer_tag_1 = (TextView) convertView.findViewById(R.id.tv_offer_tag_1);
            vh.tv_offer_tag_2 = (TextView) convertView.findViewById(R.id.tv_offer_tag_2);

            vh.iv_offer_product = (ImageView) convertView.findViewById(R.id.iv_offer_product);
            vh.tv_offer_title = (TextView) convertView.findViewById(R.id.tv_offer_title);
            vh.tv_offer_unit = (TextView) convertView.findViewById(R.id.tv_offer_unit);
            vh.tv_offer_count = (TextView) convertView.findViewById(R.id.tv_offer_count);
            vh.et_offer_price = convertView.findViewById(R.id.tv_offer_price);
            vh.iv_offer_add = convertView.findViewById(R.id.iv_offer_add);
            vh.iv_offer_minus = convertView.findViewById(R.id.iv_offer_minus);
            vh.tv_offer_price_hint = convertView.findViewById(R.id.tv_offer_price_hint);
            vh.tv_offer_state = convertView.findViewById(R.id.tv_offer_state);
            vh.tv_offer_require = (TextView) convertView.findViewById(R.id.tv_offer_require);
            vh.tv_offer_time_limit_1 = (TextView) convertView.findViewById(R.id.tv_offer_time_limit_1);
            vh.tv_offer_time_limit_2 = (TextView) convertView.findViewById(R.id.tv_offer_time_limit_2);
            vh.tv_offer_time_limit_3 = (TextView) convertView.findViewById(R.id.tv_offer_time_limit_3);
            vh.fl_offer_time_limit_1 = convertView.findViewById(R.id.fl_offer_time_limit_1);
            vh.fl_offer_time_limit_2 = convertView.findViewById(R.id.fl_offer_time_limit_2);
            vh.fl_offer_time_limit_3 = convertView.findViewById(R.id.fl_offer_time_limit_3);
            vh.tv_offer_time_limit_hint = (TextView) convertView.findViewById(R.id.tv_offer_time_limit_hint);
            vh.tv_offer_sure = (TextView) convertView.findViewById(R.id.tv_offer_sure);
            vh.watcher = new EtTextChanged(position);
            vh.et_offer_price.addTextChangedListener(vh.watcher);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        vh.watcher.setPosition(position);
        OfferListEntity.ContentBean.ProductListBean bean = list.get(position);
        ImageLoader.getInstance().displayImage(bean.getImageUrl(), vh.iv_offer_product);//imageUrl
        vh.tv_offer_title.setText(bean.getTitle());
        vh.tv_offer_unit.setText(bean.getPack());
        vh.tv_offer_count.setText("要货数量："+bean.getCount()+bean.getChargeUnit());
        vh.rl_store.setTag(R.id.item_key_position, position);
        vh.rl_store.setOnClickListener(this);
        vh.iv_offer_close.setTag(R.id.item_key_status,bean);
        vh.iv_offer_close.setTag(R.id.item_key_position, position);
        vh.iv_offer_close.setOnClickListener(this);
        vh.tv_offer_sure.setTag(R.id.item_key_status,bean.getFather());
        vh.tv_offer_sure.setTag(R.id.item_key_position,position);
        vh.tv_offer_sure.setOnClickListener(this);
        vh.v_disable.setOnClickListener(this);
        vh.fl_offer_time_limit_1.setTag(R.id.item_key_status,bean.getFather());
        vh.fl_offer_time_limit_1.setTag(R.id.item_key_position,position);
        vh.fl_offer_time_limit_1.setOnClickListener(this);
        vh.fl_offer_time_limit_2.setTag(R.id.item_key_status,bean.getFather());
        vh.fl_offer_time_limit_2.setTag(R.id.item_key_position,position);
        vh.fl_offer_time_limit_2.setOnClickListener(this);
        vh.fl_offer_time_limit_3.setTag(R.id.item_key_status,bean.getFather());
        vh.fl_offer_time_limit_3.setTag(R.id.item_key_position,position);
        vh.fl_offer_time_limit_3.setOnClickListener(this);
        vh.rl_store.setVisibility(View.GONE);
        vh.tv_offer_price_tail.setText("元/"+bean.getChargeUnit());
        if(bean.isHeader()) {
            vh.rl_store.setVisibility(View.VISIBLE);
            vh.tv_offer_store.setText(bean.getFather().getAccountName());
            vh.tv_offer_tag_1.setText(bean.getFather().getAccountTag());
            vh.tv_offer_tag_2.setText(bean.getFather().getAccountSubjectName());
            if (!TextUtils.isEmpty(bean.getFather().getAccountTag()))
                vh.tv_offer_tag_1.setVisibility(View.VISIBLE);
            else
                vh.tv_offer_tag_1.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(bean.getFather().getAccountSubjectName()))
                vh.tv_offer_tag_2.setVisibility(View.VISIBLE);
            else
                vh.tv_offer_tag_2.setVisibility(View.GONE);
            if(bean.getReminderTime()==null||bean.getReminderTime().equals("")) {
                vh.offer_time_remind.setText("剩余时间  00:00:00");
            }else{
                vh.offer_time_remind.setText("剩余时间  "+bean.getReminderTime());
            }
        }

        vh.rl_offer_footer.setVisibility(View.GONE);
        if(bean.isFoot()/*&&bean.getFather().getReminderTime()!=null&&!"".equals(bean.getFather().getReminderTime())*/){
            vh.rl_offer_footer.setVisibility(View.VISIBLE);
            vh.tv_offer_time_limit_1.setVisibility(View.GONE);
            vh.tv_offer_time_limit_2.setVisibility(View.GONE);
            vh.tv_offer_time_limit_3.setVisibility(View.GONE);
            switch (bean.getFather().getFather().getExpiredMin().size()) {
                case 1:
                    vh.tv_offer_time_limit_1.setText(bean.getFather().getFather().getExpiredMin().get(0) + "分钟");
                    vh.tv_offer_time_limit_1.setVisibility(View.VISIBLE);
                    vh.tv_offer_time_limit_1.setEnabled(true);
                    vh.tv_offer_time_limit_1.setTextColor(Color.RED);
                    break;
                case 2:
                    vh.tv_offer_time_limit_1.setText(bean.getFather().getFather().getExpiredMin().get(0) + "分钟");
                    vh.tv_offer_time_limit_2.setText(bean.getFather().getFather().getExpiredMin().get(1) + "分钟");
                    vh.tv_offer_time_limit_1.setVisibility(View.VISIBLE);
                    vh.tv_offer_time_limit_2.setVisibility(View.VISIBLE);
                    if (bean.getFather().getExpiredMinSelect().equals(bean.getFather().getFather().getExpiredMin().get(0))) {
                        vh.tv_offer_time_limit_1.setEnabled(true);
                        vh.tv_offer_time_limit_1.setTextColor(Color.RED);
                        vh.tv_offer_time_limit_2.setEnabled(false);
                        vh.tv_offer_time_limit_2.setTextColor(Color.GRAY);
                    } else {
                        vh.tv_offer_time_limit_2.setEnabled(true);
                        vh.tv_offer_time_limit_2.setTextColor(Color.RED);
                        vh.tv_offer_time_limit_1.setTextColor(Color.GRAY);
                        vh.tv_offer_time_limit_1.setEnabled(false);
                    }
                    break;
                case 3:
                    vh.tv_offer_time_limit_1.setText(bean.getFather().getFather().getExpiredMin().get(0) + "分钟");
                    vh.tv_offer_time_limit_2.setText(bean.getFather().getFather().getExpiredMin().get(1) + "分钟");
                    vh.tv_offer_time_limit_3.setText(bean.getFather().getFather().getExpiredMin().get(2) + "分钟");
                    vh.tv_offer_time_limit_1.setVisibility(View.VISIBLE);
                    vh.tv_offer_time_limit_2.setVisibility(View.VISIBLE);
                    vh.tv_offer_time_limit_3.setVisibility(View.VISIBLE);
                    vh.tv_offer_time_limit_1.setTextColor(Color.GRAY);
                    vh.tv_offer_time_limit_2.setTextColor(Color.GRAY);
                    vh.tv_offer_time_limit_3.setTextColor(Color.GRAY);
                    if (bean.getFather().getExpiredMinSelect().equals(bean.getFather().getFather().getExpiredMin().get(0))) {
                        vh.tv_offer_time_limit_1.setEnabled(true);
                        vh.tv_offer_time_limit_1.setTextColor(Color.RED);
                        vh.tv_offer_time_limit_2.setEnabled(false);
                        vh.tv_offer_time_limit_2.setTextColor(Color.GRAY);
                        vh.tv_offer_time_limit_3.setEnabled(false);
                        vh.tv_offer_time_limit_3.setTextColor(Color.GRAY);
                    }else if(bean.getFather().getExpiredMinSelect().equals(bean.getFather().getFather().getExpiredMin().get(1))){
                        vh.tv_offer_time_limit_2.setEnabled(true);
                        vh.tv_offer_time_limit_2.setTextColor(Color.RED);
                        vh.tv_offer_time_limit_1.setEnabled(false);
                        vh.tv_offer_time_limit_1.setTextColor(Color.GRAY);
                        vh.tv_offer_time_limit_3.setEnabled(false);
                        vh.tv_offer_time_limit_3.setTextColor(Color.GRAY);
                    }else if(bean.getFather().getExpiredMinSelect().equals(bean.getFather().getFather().getExpiredMin().get(3))){
                        vh.tv_offer_time_limit_3.setEnabled(true);
                        vh.tv_offer_time_limit_3.setTextColor(Color.RED);
                        vh.tv_offer_time_limit_2.setEnabled(false);
                        vh.tv_offer_time_limit_2.setTextColor(Color.GRAY);
                        vh.tv_offer_time_limit_1.setEnabled(false);
                        vh.tv_offer_time_limit_1.setTextColor(Color.GRAY);
                    }

                    break;
                default:

                    break;
            }
            vh.tv_offer_sure.setEnabled(true);
            vh.tv_offer_time_limit_hint.setTextColor(Color.parseColor("#434343"));
            if(bean.getReminderTime()==null||"".equals(bean.getReminderTime())){
                vh.tv_offer_sure.setEnabled(false);
                vh.tv_offer_time_limit_3.setEnabled(false);
                vh.tv_offer_time_limit_2.setEnabled(false);
                vh.tv_offer_time_limit_1.setEnabled(false);
                vh.tv_offer_time_limit_1.setTextColor(Color.GRAY);
                vh.tv_offer_time_limit_2.setTextColor(Color.GRAY);
                vh.tv_offer_time_limit_3.setTextColor(Color.GRAY);
                vh.tv_offer_time_limit_hint.setTextColor(Color.GRAY);
            }else{
                if(!bean.getFather().isProductEffective()){
                    vh.tv_offer_sure.setEnabled(false);
                    vh.tv_offer_time_limit_3.setEnabled(false);
                    vh.tv_offer_time_limit_2.setEnabled(false);
                    vh.tv_offer_time_limit_1.setEnabled(false);
                    vh.tv_offer_time_limit_1.setTextColor(Color.GRAY);
                    vh.tv_offer_time_limit_2.setTextColor(Color.GRAY);
                    vh.tv_offer_time_limit_3.setTextColor(Color.GRAY);
                    vh.tv_offer_time_limit_hint.setTextColor(Color.GRAY);
                }
            }
        }
        vh.v_disable.setVisibility(View.GONE);
        vh.tv_offer_state.setVisibility(View.GONE);
        if(bean.getReminderTime()==null||bean.getReminderTime().equals("")) {
            vh.v_disable.setVisibility(View.VISIBLE);
            vh.tv_offer_state.setText("放弃报价");
            vh.tv_offer_state.setVisibility(View.VISIBLE);
            vh.iv_offer_minus.setImageResource(R.mipmap.offer_minus_grey);
            vh.iv_offer_add.setImageResource(R.mipmap.offer_add_grey);
            vh.et_offer_price.setTextColor(Color.parseColor("#666666"));
            vh.tv_offer_price_hint.setVisibility(View.GONE);
        }else{
            if(bean.getProductStatus()!=1||bean.getStatus()==3){
                if(bean.getStatus()==3){
                    vh.tv_offer_state.setText("放弃报价");

                }else {
                    vh.tv_offer_state.setText("商品失效");
                }
                vh.v_disable.setVisibility(View.VISIBLE);
                vh.tv_offer_state.setVisibility(View.VISIBLE);
                vh.iv_offer_minus.setImageResource(R.mipmap.offer_minus_grey);
                vh.iv_offer_add.setImageResource(R.mipmap.offer_add_grey);
                vh.et_offer_price.setTextColor(Color.parseColor("#666666"));
                vh.tv_offer_price_hint.setVisibility(View.GONE);
            }else {
                vh.v_disable.setVisibility(View.GONE);
                vh.iv_offer_minus.setImageResource(R.mipmap.offer_minus);
                vh.iv_offer_add.setImageResource(R.mipmap.offer_add);
                vh.et_offer_price.setTextColor(Color.RED);
                if(bean.getPriceNow()>bean.getUnitPrice()){
                    vh.tv_offer_price_hint.setVisibility(View.VISIBLE);
                }else {
                    vh.tv_offer_price_hint.setVisibility(View.GONE);
                }
            }
        }
        vh.et_offer_price.setText(FormatUtil.subZeroAndDot(""+bean.getPriceNow()));

        vh.iv_offer_add.setTag(R.id.item_key_position, position);
        vh.iv_offer_add.setTag(R.id.item_key_status, bean);
        vh.iv_offer_add.setOnClickListener(this);
        vh.iv_offer_minus.setTag(R.id.item_key_position, position);
        vh.iv_offer_minus.setTag(R.id.item_key_status, bean);
        vh.iv_offer_minus.setOnClickListener(this);
        vh.et_offer_price.setTag(position);
        vh.et_offer_price.setTag(R.id.item_key_position, position);
        vh.et_offer_price.setTag(R.id.item_key_status, bean);
        vh.et_offer_price.setOnClickListener(this);

        return convertView;
    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            Integer position = (Integer) tag;
            switch (v.getId()) {
                case R.id.rl_store:
                    if (modifyDialog == null)
                        modifyDialog = new OfferBuyerDialog(context, true);
                    modifyDialog.show(list.get(position).getFather());
                    break;
                case R.id.fl_offer_time_limit_3:
                    list.get(position).getFather().setExpiredMinSelect(list.get(position).getFather().getFather().getExpiredMin().get(2));
                    notifyDataSetChanged();
                    break;

                case R.id.fl_offer_time_limit_2:
                    list.get(position).getFather().setExpiredMinSelect(list.get(position).getFather().getFather().getExpiredMin().get(1));
                    notifyDataSetChanged();
                    break;
                case R.id.fl_offer_time_limit_1:
                    list.get(position).getFather().setExpiredMinSelect(list.get(position).getFather().getFather().getExpiredMin().get(0));
                    notifyDataSetChanged();
                    break;
                case R.id.iv_offer_close:
                case R.id.tv_offer_sure:
                case R.id.iv_offer_add:
                case R.id.iv_offer_minus:
                case R.id.tv_offer_price:
                    Log.e("adapter________",""+position);
                    onItemClickListener.onItemClick(null,v,position,position);
                    break;
            }
        }
    }
    class EtTextChanged implements TextWatcher {
        private int position;

        public EtTextChanged(int position){
            this.position = position;
        }


        public void setPosition(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            //editPosition = position;
            if(TextUtils.isEmpty(s)) {
                writeContent.write(position,"");
                return;
            }
            writeContent.write(position,s.toString().trim());
        }
    }
    public interface WriteContent{
        void write(int position, String content);
    }
    private WriteContent writeContent;
    public void setWriteContent(WriteContent writeContent) {
        this.writeContent = writeContent;
    }
    public class ViewHolder {
        TextView offer_time_remind;

        View rl_store,iv_offer_close,rl_offer_footer,v_disable;
        TextView tv_offer_store,tv_offer_tag_1,tv_offer_tag_2;
        ImageView iv_offer_right,iv_offer_state;
        TextView tv_offer_title,tv_offer_unit,tv_offer_count,tv_offer_require;
        TextView et_offer_price;
        ImageView iv_offer_product,iv_promotion;
        TextView tv_offer_time_limit_1,tv_offer_time_limit_2,tv_offer_time_limit_3,tv_offer_time_limit_hint,tv_offer_sure;
        View fl_offer_time_limit_1,fl_offer_time_limit_2,fl_offer_time_limit_3;
        ImageView iv_offer_add,iv_offer_minus;
        TextView tv_offer_state,tv_offer_price_hint;
        EtTextChanged watcher;

        TextView tv_offer_price_tail;
    }

    private OfferBuyerDialog modifyDialog;
    public Timer timer;
    boolean isTimerAlive;
    TimerTask task ;
    Handler handler = new Handler(){

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    notifyDataSetChanged();
                    break;
            }
            super.handleMessage(msg);
        }

    };

    public void timerStart() {
        timerStop();
        if(timer == null)
            timer = new Timer();

        task =  new TimerTask() {
            @Override
            public void run() {
                try {


                    if (isTimerAlive){
                        Message message = Message.obtain();
                        message.what = 1;
                        handler.sendMessage(message);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        isTimerAlive = true;

        timer.schedule(task, 0, 1000);
    }


    public void timerStop() {
        isTimerAlive = false;
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (task != null){
            task.cancel();
            task = null;
        }
    }

}
